package com.t3.display
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;

	public class Thumbnail extends Bitmap
	{
		private var _timer:Timer;
		private var _big:DisplayObject;
		private var _bounds:Rectangle;
		private var _transparent:Boolean;
		private var clearGraphics:Boolean;
		
		public function Thumbnail( bounds:Rectangle, big:DisplayObject, scale:Number = 1/8, autoUpdate:Boolean = true, transparent:Boolean = false, clearGraphics:Boolean = false ):void
		{
			this.clearGraphics = clearGraphics;
			_transparent = transparent;
			_bounds = bounds;
			_big = big;
			
			this.bitmapData = new BitmapData( bounds.width, bounds.height, transparent, 0xFFFFFF );
			this.scaleX = this.scaleY = scale;
			
			if( autoUpdate )
			{	
				_timer = new Timer( 1000 );
				_timer.addEventListener( TimerEvent.TIMER, autoUpdater );
				_timer.start();
			}
		}
		
		private function autoUpdater( event:TimerEvent ):void
		{
			updateThumb();
		}
		
		public function updateThumb():void
		{
			if( clearGraphics )
			{
				this.clear();
			}
			
			var tmpMask:DisplayObject = _big.mask;
			_big.mask = null;
			this.bitmapData.draw( _big );
			_big.mask = tmpMask;
		}
		
		public function clear():void
		{
			this.bitmapData = new BitmapData( _bounds.width, _bounds.height, _transparent, 0xFFFFFF );
		}
	}
}